﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Collections.Generic;

namespace Xceed.Silverlight.ListBox
{
  public class ListBoxGroupHeaderConfiguration : ListBoxContainerConfiguration<ListBoxGroupHeader>
  {
    #region CONSTRUCTORS

    public ListBoxGroupHeaderConfiguration()
      : base()
    {
    }

    protected ListBoxGroupHeaderConfiguration( ListBoxGroupHeaderConfiguration template )
      : base( template )
    {
      m_groupValueDataTemplates = template.GroupValueDataTemplates;
    }

    #endregion CONSTRUCTORS

    #region CanDrag Property

    internal override sealed bool CanDrag
    {
      get
      {
        return false;
      }
    }

    #endregion CanDrag Property

    #region CanStick Property

    public override bool CanStick
    {
      get
      {
        return true;
      }
    }

    #endregion CanStick Property

    #region GroupValueDataTemplates
    public List<GroupValueDataTemplate> GroupValueDataTemplates
    {
      get
      {
        return m_groupValueDataTemplates;
      }
    }
    #endregion

    #region PROTECTED METHODS

    protected override ListBoxGroupHeader CreateContainerCore()
    {
      return new ListBoxGroupHeader();
    }

    protected internal override ListBoxContainerConfigurationBase CreateInstance()
    {
      return new ListBoxGroupHeaderConfiguration( this );
    }

    protected override void PrepareContainerForItemCore( ListBoxGroupHeader container, object item )
    {
      base.PrepareContainerForItemCore( container, item );

      // Sets the GroupValueDataTemplate on the container so the HierarchicalGroupValueControl
      // can use them to with the ContentTemplate selector. 
      List<GroupValueDataTemplate> groupValueDataTemplates = this.GroupValueDataTemplates;

      if( groupValueDataTemplates != null )
      {
        container.GroupValueDataTemplates = groupValueDataTemplates;
      }
    }

    protected override void EndInitCore()
    {
      base.EndInitCore();

      // Validate GroupValueDataTemplates
      // Make sure a field name is only associted with one DataTemplate.
      List<string> fieldNames = new List<string>();

      foreach( GroupValueDataTemplate groupValueDataTemplate in this.GroupValueDataTemplates )
      {
        foreach( string fieldName in groupValueDataTemplate.FieldNames )
        {
          if( fieldNames.Contains( fieldName ) )
          {
            throw new ListBoxException( "The field name is already associated with a DataTemplate." );
          }
          fieldNames.Add( fieldName );
        }
      }
    }

    #endregion PROTECTED METHODS

    #region INTERNAL METHODS

    internal override ConfigurationType GetConfigurationType()
    {
      return ConfigurationType.GroupHeader;
    }

    #endregion

    #region Private Members
    List<GroupValueDataTemplate> m_groupValueDataTemplates = new List<GroupValueDataTemplate>();
    #endregion
  }
}
